package xq.gwt.ui.widgets.property;


import xq.gwt.mvc.controller.EntityArrayPropertyController;
import xq.gwt.mvc.model.EntityArrayPropertyModel;
import xq.gwt.mvc.model.EntityModel;
import xq.gwt.mvc.view.EntityArrayPropertyView;
import xq.gwt.ui.widgets.entity.EntityArrayPresenter;
import xq.gwt.ui.widgets.entity.EntitySelectedListener;

import com.smartgwt.client.types.ListGridEditEvent;
import com.smartgwt.client.widgets.form.fields.CanvasItem;
import com.smartgwt.client.widgets.form.fields.FormItem;
import com.smartgwt.client.widgets.form.fields.events.FormItemInitHandler;
import com.smartgwt.client.widgets.grid.ListGrid;

public class EntityListItemPropertyPresenter extends FormItemPropertyPresenter implements EntityArrayPropertyView, EntitySelectedListener {
	protected ListGrid grid;
	protected EntityArrayPropertyController controller;
	protected EntityArrayPresenter entityPresenter;
	private EntitySelectedListener selectionListener;

	public EntityListItemPropertyPresenter(ListGrid grid) {
		this.grid = grid;		
		init();
	}

	public EntityListItemPropertyPresenter() {
		this.grid = new ListGrid();		
		init();
	}

	protected void init() {		
		
		controller = new EntityArrayPropertyController(this);
		super.controller = controller;
		
	}

	
	public void setModel(EntityArrayPropertyModel model) {
		if(model != null && model.getPrototype() != null){
			entityPresenter = new EntityArrayPresenter(grid, model.getPrototype());
			grid.setCanEdit(!model.isReadonly());
			grid.setEditByCell(true);
			grid.setEditEvent(ListGridEditEvent.CLICK);
		} else {
			entityPresenter = new EntityArrayPresenter(grid);
		}
		
		entityPresenter.setSelectionListener(this);
		
		formItem = new ListGridItem(grid);		
		controller.setPropertyModel(model);				
	}

	
	
	@Override
	public void setEnabled(boolean enabled) {
		grid.setCanEdit(enabled);		
	}

	@Override
	public EntityModel[] getEntities() {		
		return null;
	}

	@Override
	public void setEntities(EntityModel[] val) {
		entityPresenter.setEntities(val);
		
	}
	
	
	public void setEntitySelectedListener(EntitySelectedListener selectionListener) {
		this.selectionListener = selectionListener;
	}

	@Override
	public void entitySelected(EntityModel entity) {
		if(selectionListener != null){
			selectionListener.entitySelected(entity);
		}		
	}

	public EntityArrayPresenter getEntityArrayPresenter(){
		return entityPresenter;
	}
	
	public class ListGridItem extends CanvasItem {  
        protected ListGrid grid;
		public ListGridItem (ListGrid grid) {  
            super();  
            this.grid = grid;  
            setHeight("*");
            setWidth("*");              
            setEndRow(true);  
            setStartRow(true);  
            setColSpan("*");  
            setShowTitle(false);             
            setInitHandler(new InitHandler(grid));
        }  
     
		private class InitHandler implements FormItemInitHandler {
			private ListGrid grid;
			
			public InitHandler(ListGrid grid){
				this.grid = grid;
			}
			
			@Override
			public void onInit(FormItem item) {
				grid.setWidth("*");  
                grid.setHeight("*");  
                grid.setLeaveScrollbarGap(false);                                         
                ((CanvasItem) item).setCanvas(grid);
				
			}
			
		}
                  
    }  
	

}
